package cn.ailearn.security.details;

import cn.ailearn.module.pojo.entity.Users;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @ClassName MySysUserDetails
 * @Description 自定义用户详情类
 * @Author WDEMO
 * @Date 2025/10/22 18:04
 */
@Slf4j
@Data
@AllArgsConstructor
public class MySysUserDetails implements UserDetails {


    /**
     * 用户信息
     */
    private Users users;

    /**
     * 权限信息
     */
    private Set<String> permissions;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        log.info("进入权限的获取方法");
        if (permissions==null) {
            return null;
        }
        List<GrantedAuthority> authorities=new ArrayList<>();
         permissions.stream().map(permission -> new SimpleGrantedAuthority(permission)).forEach(authorities::add);
        return authorities;
    }

    @Override
    public String getPassword() {
        return users.getPassword();
    }

    @Override
    public String getUsername() {
        return users.getUsername();
    }
}
